/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultarUsuarioGUI.java
 *
 * Created on 26/07/2011, 11:33:43
 */

package carrancao.gui;

import carrancao.controlador.Fachada;
import carrancao.entidades.Cliente;
import carrancao.entidades.Usuario;
import carrancao.util.UsuarioTableModel;
import java.awt.Cursor;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

/**
 *
 * @author Lubnnia
 */
public class ConsultarUsuarioGUI extends javax.swing.JFrame {

    private List<Usuario> listUsuarios = new ArrayList<Usuario>();
    private UsuarioTableModel usuarioTableModel;

    /** Creates new form ConsultarUsuarioGUI */
    public ConsultarUsuarioGUI() {
        initComponents();
        setLocationRelativeTo(null);
        refreshTable(listUsuarios);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupPesquisa = new javax.swing.ButtonGroup();
        jTextFieldCampoPesquisa = new javax.swing.JTextField();
        jButtonPesquisar = new javax.swing.JButton();
        jRadioButtonCodigo = new javax.swing.JRadioButton();
        jRadioButtonNome = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableUsuarios = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta de Usuários");

        jTextFieldCampoPesquisa.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jButtonPesquisar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonPesquisar.setText("Pesquisar");
        jButtonPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonPesquisarActionPerformed(evt);
            }
        });

        buttonGroupPesquisa.add(jRadioButtonCodigo);
        jRadioButtonCodigo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRadioButtonCodigo.setText("Código");

        buttonGroupPesquisa.add(jRadioButtonNome);
        jRadioButtonNome.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRadioButtonNome.setSelected(true);
        jRadioButtonNome.setText("Nome");

        jTableUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTableUsuarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableUsuariosMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jTableUsuariosMouseEntered(evt);
            }
        });
        jScrollPane1.setViewportView(jTableUsuarios);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton1.setText("Fechar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 740, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextFieldCampoPesquisa, javax.swing.GroupLayout.DEFAULT_SIZE, 477, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonCodigo)
                                .addGap(18, 18, 18)
                                .addComponent(jRadioButtonNome)
                                .addGap(18, 18, 18)
                                .addComponent(jButtonPesquisar))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(291, 291, 291)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCampoPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButtonNome)
                    .addComponent(jRadioButtonCodigo))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(87, 87, 87)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(95, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void limparCampos() {
        jTextFieldCampoPesquisa.setText("");
    }
    
    private void refreshTable(List<Usuario> list) {
        usuarioTableModel = new UsuarioTableModel(list);
        jTableUsuarios.setModel(usuarioTableModel);
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButtonPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesquisarActionPerformed
        try {
            if(jRadioButtonCodigo.isSelected()) {
                Usuario u = null;
                u = Fachada.getInstance().getUsuarioControl().pesquisarUsuarioPorID(Integer.parseInt(jTextFieldCampoPesquisa.getText()));
                
                if(u != null) {
                    //List<Cliente> listClientes = new ArrayList<Cliente>();
                    listUsuarios.add(u);
                    refreshTable(listUsuarios);
                } else {
                    JOptionPane.showMessageDialog(null, "Código inválido!", "Mensagem do Sistema", JOptionPane.ERROR_MESSAGE);
                }
                limparCampos();

          } else if(jRadioButtonNome.isSelected()) {
              //List<Usuario> listUsuarios = new ArrayList<Usuario>();
              listUsuarios = Fachada.getInstance().getUsuarioControl().pesquisarUsuarioPorNome(jTextFieldCampoPesquisa.getText());
              
              if(!(listUsuarios.isEmpty())) {
                  refreshTable(listUsuarios);
              } else {
                  JOptionPane.showMessageDialog(null, "Este usuário não existe!", "Mensagem do Sistema", JOptionPane.ERROR_MESSAGE);
              }
              limparCampos();
          }

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Nenhum usuário encontrado!", "Mensagem do Sistema", JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_jButtonPesquisarActionPerformed

    private void jTableUsuariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableUsuariosMouseClicked
        if(evt.getClickCount() == 2) {
            int linha = jTableUsuarios.getSelectedRow();

            AlterarUsuarioGUI tela = new AlterarUsuarioGUI();
            tela.setUsuarios(usuarioTableModel.getUsuario(linha));
            tela.setVisible(true);
        }
    }//GEN-LAST:event_jTableUsuariosMouseClicked

    private void jTableUsuariosMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableUsuariosMouseEntered
        jTableUsuarios.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }//GEN-LAST:event_jTableUsuariosMouseEntered

    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConsultarUsuarioGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroupPesquisa;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonPesquisar;
    private javax.swing.JRadioButton jRadioButtonCodigo;
    private javax.swing.JRadioButton jRadioButtonNome;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableUsuarios;
    private javax.swing.JTextField jTextFieldCampoPesquisa;
    // End of variables declaration//GEN-END:variables

}
